import {
	formKeys
} from "../report.form.config"
import {
	useHandleType,
	useGetEventGrid,
	useGetEventRole,
	useGetEventDept,
	useGetEventUser,
	useGetEventGrandson
} from "../hooks/useEventNextHandler"
import {
	HandleMap
} from "@/enums/report"
import {
	toHump
} from '../../../../common/utils';

export default {
	data() {
		return {
			eventHandlerShow: false,
			mhCheckPath: "",
			taskId: "",
			parents: null,
			mhLevel: 0, // 选择的层级
		}
	},
	methods: {
		async mhCheckHandler() {
			if (uni.$u.test.empty(this.taskId)) {
				this.$toast("请选择带有流程的事件类型")
				return;
			}
			if (uni.$u.test.empty(this.form[formKeys.lat])) {
				this.$toast("请选择纬度")
				return;
			}
			if (uni.$u.test.empty(this.form[formKeys.lon])) {
				this.$toast("请选择经度")
				return;
			}
			if (!this.parents) {
				const {
					code,
					data = null
				} = await useHandleType({
					taskId: this.taskId,
					lat: this.form[formKeys.lat],
					lon: this.form[formKeys.lon],
				}, 0)
				if (code === 200) {
					this.parents = data;
					this.$refs.RefHandleCascader.push(data, this.mhVerify);
				}
			}
			this.eventHandlerShow = true;
		},
		/**
		 * @description 下一节点处理人级联组件点选回调
		 * @param {Number} level 当前选择的层级
		 * @param {Object} e 当前选择的数据对象
		 * 
		 */
		async mhCheckEventConfirm({
			level,
			e,
			navs
		}) {
			this.mhLevel = level;
			let list = [];
			const parent = navs[0];
			if (level === 0) {
				// e.value
				switch (e._taskType) {
					case HandleMap.grid:
						list = await useGetEventGrid(e.source)
						break;
					case HandleMap.role:
						list = await useGetEventRole(e.source)
						break;
					case HandleMap.dept:
						list = await useGetEventDept(e.source)
						break;
					case HandleMap.user:
						list = await useGetEventUser(e.source)
						break;
					default:
						break;
				}
			} else if (e._taskType === HandleMap.dept && e.children && e.children.length > 0) {
				list = e.children ?? []
				list.forEach(item => {
					Object.keys(item).map(key => this.$set(item, toHump(key), item[key]));
				})
				list = await useGetEventDept(list)
			} else if (e._taskType !== HandleMap.user) {
				const {
					code,
					data
				} = await useGetEventGrandson({
					bs: parent.value,
					id: e.value
				})
				list = data ?? []
			}
			console.log("子孙节点", list, level);
			this.$refs.RefHandleCascader.push(list, this.mhVerify);
		},
		// 当前层级,本次填充的数据,当前路径集合
		mhVerify(level, array, navs) {
			console.log("mhVerify", navs);
			if ((level === 0 || navs[0].taskType?.toString() === HandleMap.dept.toString()) && (!array || array.length === 0)) {
				this.$toast("当前节点下无可选数据")
				return false;
			}
			return true
		},
		mhChooseConfirm({
			chooseItem,
			choosePath,
			path,
			level
		}) {
			this.mhLevel = level;
			console.log("this.mhLevel", this.mhLevel);
			if (level === 0) {
				this.$toast("当前节点下无可选数据");
				return;
			}
			const parent = choosePath[0];
			this.form[formKeys.eventHandler] = chooseItem.user_id?.toString();
			this.form[formKeys.eventType] = parent.value;
			this.mhCheckPath = path;
			// this.mhCheckPath = choosePath[choosePath.length - 1].label;
			console.log("mhChooseConfirm", chooseItem, choosePath, JSON.parse(JSON.stringify(this.form)));
		},

	}
}



/**
 * 一.网格 taskDealType: "1"
 *    数据源:taskData.data数组 [{grid_id:数据id,grid_name:数据名称}]
 * 二.角色 taskDealType: "2"
 *    数据源:taskData.data数组 [{roleId:数据id,roleName:数据名称}]
 * 三.部门 taskDealType: "3"
 *   数据源:taskData.data数组 [{deptId:数据id,deptName:数据名称}]
 * 四.人员 taskDealType: "4"  不用再请求了
 *   数据源:taskData.data数组 [{user_id:数据id,user_name:数据名称}]
 */